﻿

!function () {
  var w = this,
    n = w._fn_write2 || 'write2';

  w[n] ? w[n](0, 1) :
    function () {
      var usage,
        d = document,
        regex = /^([\s\S]*?<\/script[^>]*>)([\s\S]*)$/i,
        l = '<script type="text/ecmascript" src="',
        r = '" write2-agent="1"></script>',
        str = '',
        pre = '',
        fire = 1;

      d.write(l + 'data:,_data_uri_useable=1' + r);

      function write2(v, run) {
        if (!usage) usage = l + (w._data_uri_useable ?
            'data:,' + n + '(0,1)' :
            function () {
              var c = d.getElementsByTagName('script'),
                i = c.length,
                addr = w._src_write2 || n,
                h;
              while (i-- > 0) {
                if ((h = c[i].getAttribute('src')) && h.indexOf(addr) >= 0)
                  return encodeURI(h);
              }
              return addr;
            } ()
          ) + r;

        if (run) {
          fire = 1;

          v = str + pre;
          pre = str = '';

          if (v) d.write(v.replace(regex, function (m, a, b, i, h) {
            pre = b;
            fire = 0;
            return a + usage;
          }));
        } else {
          str += v;
          if (fire) fire = 0, d.write(usage);
        }

      }

      w[n] = d[n] = write2;
    } ();

} ();